package ideamaps.action;

import java.util.Date;
import java.util.List;

import ideamaps.domain.Request;
import ideamaps.domain.User;
import ideamaps.manager.FriendManager;
import ideamaps.manager.UserManager;

import com.opensymphony.xwork2.Preparable;
import com.taillive.core.dao.support.Page;
import com.taillive.core.web.StrutsAction;

public class FriendAction extends StrutsAction{
	
	private FriendManager friendManager;
	public void setFriendManager(FriendManager friendManager) {
		this.friendManager = friendManager;
	}
	
	private UserManager userManager;
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	
	
	private Long id;
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}
	
	
	private Long ids[];
	public Long[] getIds() {
		return ids;
	}

	public void setIds(Long[] ids) {
		this.ids = ids;
	}
	
	private Page p;
	public Page getP() {
		return p;
	}
	
	
	private int page;
	public void setPage(int page) {
		this.page = page;
	}

	public String list(){
		
		String username = (String)getSession().getAttribute("sessionUsername");
		User user = userManager.getByUsername(username);
		
		p = friendManager.getPageByUserId(user.getId(),page, 1);
		p.setUrl("myfriends.jspa");
		
		return SUCCESS;
	}


	private String username;
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
	
	private String message ;
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String addFriend(){
		String sessionUsername = (String)getSession().getAttribute("sessionUsername");
		if(sessionUsername==null){
			return "login";
		}
		
		friendManager.addFriend(username, sessionUsername, message);
		return SUCCESS;
	}
	
	
	private List<Request> reqs;
	public List<Request> getReqs() {
		return reqs;
	}
	

	public String requests() {
		String sessionUsername = (String)getSession().getAttribute("sessionUsername");
		reqs = friendManager.getRequestsByUsername(sessionUsername);
		return SUCCESS;
	}
	
	
	private long requestId;
	public void setRequestId(long requestId) {
		this.requestId = requestId;
	}
	
	
	public String approveRequest() {
		friendManager.approveRequest(requestId);
		getSession().removeAttribute("requestsCount");
		return SUCCESS;
	}
	
	
	public String ignoreRequest() {
		friendManager.ignoreRequest(requestId);
		return SUCCESS;
	}

}
